package com.czf.personal.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czf.common.entity.*;
import com.czf.common.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author chenzhanfan
 * @Date 2022/4/22
 * @Description
 */
@Service
@Slf4j
public class ModelServiceImpl {

    @Autowired
    private UserBaseInfoMapper userBaseInfoMapper;
    @Autowired
    private UserEconomicCapacityMapper userEconomicCapacityMapper;
    @Autowired
    private FilePathInfoMapper filePathInfoMapper;
    @Autowired
    private RecordUserBankMapper recordUserBankMapper;
    @Autowired
    private RecordUserSoftScoreMapper recordUserSoftScoreMapper;
    @Autowired
    private ScoreProportionMapper scoreProportionMapper;

    static List<String> occupationList = Arrays.asList(
            new String[]{"国家机关单位", "军人", "专业技术人员", "商业/服务业", "农/林/牧/渔/水利业",
                    "生产/运输设备操作人员及有关人员", "办事人员和有关人员", "特殊职业的其他从业人员"});

    static List<String> educationList = Arrays.asList(new String[]{"初中", "高中", "大专", "大学本科", "研究生及以上"});
    static List<String> workTypeList = Arrays.asList(new String[]{"国企", "外企", "私企"});
    static List<String> liveInfoList = Arrays.asList(new String[]{"租房", "自建房", "已购房"});

    public int getUserBaseInfoScore(Long userId) {
        int score = 0;
        UserBaseInfo userBaseInfo = userBaseInfoMapper.selectById(userId);
        if (ObjectUtils.isNotEmpty(userBaseInfo)) {
            for (int i = 0; i < occupationList.size(); i++) {
                if (occupationList.get(i).equals(userBaseInfo.getOccupation())) {
                    score += occupationList.size() - i;
                    break;
                }
            }
            for (int i = 0; i < educationList.size(); i++) {
                if (educationList.get(i).equals(userBaseInfo.getEducation())) {
                    score += i + 1;
                    break;
                }
            }
            for (int i = 0; i < workTypeList.size(); i++) {
                if (workTypeList.get(i).equals(userBaseInfo.getWorkType())) {
                    score += workTypeList.size() - i;
                    break;
                }
            }
            if (userBaseInfo.getMarriage().equals(true)) {
                score += 2;
            } else {
                score += 1;
            }

            for (int i = 0; i < liveInfoList.size(); i++) {
                if (liveInfoList.get(i).equals(userBaseInfo.getLiveInfo())) {
                    score += i;
                }
            }
        }
        return score * 5;
    }

    public int getEconomicScore(Long userId) {
        int score = 0;
        UserEconomicCapacity userEconomicCapacity = userEconomicCapacityMapper.selectById(userId);
        if (ObjectUtils.isNotEmpty(userEconomicCapacity)) {
            Integer salary = userEconomicCapacity.getSalary() + 1;
            Integer meritPay = userEconomicCapacity.getMeritPay() + 1;
            Integer otherIncome = userEconomicCapacity.getOtherIncome() + 1;
            Boolean fiveOne = userEconomicCapacity.getFiveOne();
            Integer annualBonus = userEconomicCapacity.getAnnualBonus() + 1;
            Integer deposit = userEconomicCapacity.getDeposit() + 1;
            score = salary * 2 + meritPay * 2 + otherIncome + annualBonus * 2 + deposit * 2;
            if (fiveOne) {
                score += 3;
            } else {
                score += 1;
            }
        }
        return score * 2;
    }

    public int getLiveScore(Long userId) {
        int score = 0;
        QueryWrapper<FilePathInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("status", "审核通过");
        List<FilePathInfo> filePathInfos = filePathInfoMapper.selectList(wrapper);
        int size = filePathInfos.size();
        if (size >= 1 && size <= 4) {
            score = 25;
        } else if (size >= 5 && size <= 7) {
            score = 50;
        } else if (size >= 8 && size < 10) {
            score = 75;
        } else if (size > 10) {
            score = 100;
        }
        return score;
    }

    public int getBankScore(Long userId) {
        QueryWrapper<RecordUserBank> wrapper = new QueryWrapper<>();
        wrapper.select("SUM(good_bank) as goodNum,SUM(bad_bank) as badNum");
        wrapper.eq("user_id", userId);
        List<Map<String, Object>> maps = recordUserBankMapper.selectMaps(wrapper);
        Map<String, Object> map = maps.get(0);
        int good = 0;
        int bad = 0;
        if (ObjectUtils.isNotEmpty(map)) {
            if (ObjectUtils.isNotEmpty(map.get("goodNum"))) {
                good = Integer.parseInt(map.get("goodNum").toString());
            }
            if (ObjectUtils.isNotEmpty(map.get("badNum"))) {
                bad = Integer.parseInt(map.get("badNum").toString());
            }
        }
        if (bad >= 2) {
            return 0;
        } else if (bad == 0 && good >= 6) {
            return 100;
        } else if (bad == 0 && good >= 2 && good <= 5) {
            return 50 + 5 * good;
        } else if (good >= 6) {
            BigDecimal b = new BigDecimal((float) (good - 2 * bad) / good);
            Double result = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            return (int) (result * 100);
        }

        return 0;
    }

    public int getSoftwareScore(Long userId) {
        float score = 0;
        QueryWrapper<RecordUserSoftScore> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<RecordUserSoftScore> softScores = recordUserSoftScoreMapper.selectList(wrapper);
        if (ObjectUtils.isNotEmpty(softScores)) {
            Integer reduce = softScores.stream().map(RecordUserSoftScore::getReliability).collect(Collectors.toList()).stream().reduce(0, (x, y) -> x + y);
            for (RecordUserSoftScore softScore : softScores) {
                Float reliability = Float.valueOf(softScore.getReliability());
                Float softwareScore = Float.valueOf(softScore.getScore());
                float a = reliability / reduce;
                log.info("值为：", a);
                BigDecimal bigDecimal = new BigDecimal((float) (reliability / reduce) * softwareScore * 20);
                Double result = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                score += result;
            }
        }
        return (int) score;
    }

    public float getScore(Long userId) {
        float baseInfoScore = getUserBaseInfoScore(userId);
        float economicScore = getEconomicScore(userId);
        float liveScore = getLiveScore(userId);
        float bankScore = getBankScore(userId);
        float softwareScore = getSoftwareScore(userId);
        ScoreProportion selectOne = scoreProportionMapper.selectOne(null);
        Float baseinfo = selectOne.getBaseinfo();
        Float economic = selectOne.getEconomic();
        Float live = selectOne.getLive();
        Float bank = selectOne.getBank();
        Float software = selectOne.getSoftware();
        float score =
                (baseInfoScore * baseinfo
                        + economicScore * economic
                        + liveScore * live
                        + bankScore * bank
                        + softwareScore * software) / 100;
        return score;
    }
}
